



* NOTE: randomization to tables is essential to distinguish causal from omitted variables sources of dependence.
* NOTE: the eandetabar_dataset.dta file contains the e and etabar random effect point estimates and the full persuasion_v12.dta merged in
* NOTE: the file persuasion_v12.dta DROPS all participants with either an invalid table number (e.g., zero) or if they have fewer than five people at their table.



************ the POLICY-SPECIFIC RANDOM EFFECT test *****************

use "C:\research\bayes\funglee\persuasion\OBOE\eandetabar_dataset.dta", clear

polychoric oboe_PT_EVAL_INFO oboe_PT_EVAL_RESPECT oboe_PT_EVAL_RHEARD oboe_PT_EVAL_NOBIAS oboe_PT_EVAL_REASON oboe_PT_EVAL_OPPSPEAK oboe_PT_EVAL_DUTY oboe_PT_EVAL_CHANGE oboe_PT_EVAL_AGREE oboe_PT_EVAL_INFLUENCE oboe_PT_EVAL_FUN oboe_PT_EVAL_AGAIN
polyfactor
rotate
*predict f1 f2

* two factor solution:
*   -------------------------------------------------
*        Variable |  Factor1   Factor2 |   Uniqueness 
*    -------------+--------------------+--------------
*    oboe_PT_EV~O |   0.7141    0.1915 |      0.4534  1
*    oboe_PT_EV~T |   0.0712    0.8507 |      0.2713  2
*    oboe_PT_EV~D |   0.2232    0.7939 |      0.3199  2
*    oboe_PT_EV~S |   0.6473    0.3959 |      0.4242  1
*    oboe_PT_E~ON |   0.2878    0.7399 |      0.3696  2
*    oboe_PT_EV~K |   0.1043    0.7747 |      0.3889  2
*    oboe_PT_EV~Y |   0.4878    0.3577 |      0.6341  0
*    oboe_PT_E~GE |   0.7326   -0.0248 |      0.4626  1
*    oboe_PT_E~EE |   0.7446    0.1031 |      0.4350  1
*    oboe_PT_E~CE |   0.8110    0.1735 |      0.3122  1
*    oboe_PT_E~UN |   0.6191    0.4846 |      0.3818  0
*    oboe_PT_E~IN |   0.6612    0.4143 |      0.3912  0
*    -------------------------------------------------
* f1 = oboe_PT_EVAL_INFO oboe_PT_EVAL_NOBIAS oboe_PT_EVAL_CHANGE oboe_PT_EVAL_AGREE oboe_PT_EVAL_INFLUENCE 
* f2 = oboe_PT_EVAL_RESPECT oboe_PT_EVAL_RHEARD oboe_PT_EVAL_REASON oboe_PT_EVAL_OPPSPEAK 

* high values means informative
polychoric oboe_PT_EVAL_INFO oboe_PT_EVAL_NOBIAS oboe_PT_EVAL_CHANGE oboe_PT_EVAL_AGREE oboe_PT_EVAL_INFLUENCE
polyfactor
predict f1a

* high values means good process
polychoric oboe_PT_EVAL_RESPECT oboe_PT_EVAL_RHEARD oboe_PT_EVAL_REASON oboe_PT_EVAL_OPPSPEAK
polyfactor
predict f1b

* high values means enjoyed discussion
polychoric oboe_PT_EVAL_FUN oboe_PT_EVAL_AGAIN oboe_PT_EVAL_DUTY
polyfactor
predict f2

* high values are high internal efficacy
polychoric oboe_PR_EFF_WELLQUAL oboe_PR_EFF_WELLINFO
polyfactor
predict f3

* high values are low external efficacy
polychoric oboe_PR_EFF_NOCARE oboe_PR_EFF_NOSAY oboe_PR_GOVTRUST
polyfactor
predict f4

egen e1sd=std(e1)
egen e2sd=std(e2)
egen e3sd=std(e3)
egen e4sd=std(e4)
egen e5sd=std(e5)
egen e6sd=std(e6)

gen e1x=abs(e1)
gen e2x=abs(e2)
gen e3x=abs(e3)
gen e4x=abs(e4)
gen e5x=abs(e5)
gen e6x=abs(e6)

egen e1xsd=std(e1x)
egen e2xsd=std(e2x)
egen e3xsd=std(e3x)
egen e4xsd=std(e4x)
egen e5xsd=std(e5x)
egen e6xsd=std(e6x)


egen f1asd=std(f1a)
egen f1bsd=std(f1b)
egen f2sd=std(f2)
egen f3sd=std(f3)
egen f4sd=std(f4)


* the variable ideologyx is contained in the persuasion_v12.dta file and is constructed from
* egen ideologyx=std(latent_pre) 
gen conservative=ideologyx>.3438691
replace conservative=. if ideologyx==.
gen liberal=ideologyx<-.5483488
replace liberal=. if ideologyx==.


gen demxf1a=liberal*f1a
gen repxf1a=conservative*f1a
gen demxf1b=liberal*f1b
gen repxf1b=conservative*f1b

gen demxf1asd=liberal*f1asd
gen repxf1asd=conservative*f1asd
gen demxf1bsd=liberal*f1bsd
gen repxf1bsd=conservative*f1bsd




*** final analyses in results table (standardized results):
xi: xtreg etabarsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool democrat republican demxf1asd repxf1asd demxf1bsd repxf1bsd i.oboe_PR_FAMINC i.site, i(sitetable)




*** final analyses in results table (standardized results):

xi: xtreg e1sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e2sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e3sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e4sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e5sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e6sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)

xi: xtreg e1xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e2xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e3xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e4xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e5xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)
xi: xtreg e6xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site, i(sitetable)


xtreg e1sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
xtreg e2sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
xtreg e3sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
xtreg e4sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
xtreg e5sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
xtreg e6sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, i(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd

reg e1xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
reg e2xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
reg e3xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
reg e4xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
reg e5xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd
reg e6xsd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool i.oboe_PR_FAMINC i.site republican democrat demxf1asd repxf1asd demxf1bsd repxf1bsd, cluster(sitetable)
lincom f1asd+demxf1asd
lincom f1asd+repxf1asd


* and look at interactions between African American demo and discourse
gen f1bsdxblack=f1bsd*black
gen f2sdxblack=f1bsd*black

*but first note f1bsdxblack and f2sdxblack are exactly correlated rho = 1
pwcorr f2sdxblack  f1bsdxblack

* and then note that the coefs on the interaction terms are all zero, except possibly for the defense question
xtreg e1sd f1asd f1bsd f2sd f3sd f4sd black hispanic asian gradschool f1asdxblack f1bsdxblack f2sdxblack, i(sitetable)
* etc.





******************** START OF DESCRIPTIVE POLARIZATION STUDY *****************************************

use "C:\research\bayes\funglee\persuasion\OBOE\replication\persuasion_v12.dta", clear


gen raw_diff_taxrich=oboe_PT_DEF_TAXRICH - oboe_PR_DEF_TAXRICH
gen raw_diff_cutprog=oboe_PT_DEF_CUTPROG - oboe_PR_DEF_CUTPROG
gen raw_diff_entitle=oboe_PT_DEF_ENTITLE - oboe_PR_DEF_ENTITLE
gen raw_diff_defense=oboe_PT_DEF_DEFENSE - oboe_PR_DEF_DEFENSE

polychoric oboe_PT_DEF_TAXRICH oboe_PT_DEF_CUTPROG oboe_PT_DEF_ENTITLE oboe_PT_DEF_DEFENSE 
polyfactor
predict ideology_post
gen raw_diff_ideology = ideology_post-ideology


summ raw_diff_taxrich raw_diff_cutprog raw_diff_entitle raw_diff_defense

reg raw_diff_taxrich lib_atlibtable con_atcontable, cluster(sitetable)
reg raw_diff_cutprog lib_atlibtable con_atcontable, cluster(sitetable)
reg raw_diff_entitle lib_atlibtable con_atcontable, cluster(sitetable)
reg raw_diff_defense lib_atlibtable con_atcontable, cluster(sitetable)

xi: xtreg raw_diff_taxrich f3 f4 black hispanic asian gradschool i.oboe_PR_FAMINC i.site ideology lib_atlibtable con_atcontable, i(sitetable)
xi: xtreg raw_diff_cutprog f3 f4 black hispanic asian gradschool i.oboe_PR_FAMINC i.site ideology lib_atlibtable con_atcontable, i(sitetable)
xi: xtreg raw_diff_entitle f3 f4 black hispanic asian gradschool i.oboe_PR_FAMINC i.site ideology lib_atlibtable con_atcontable, i(sitetable)
xi: xtreg raw_diff_defense f3 f4 black hispanic asian gradschool i.oboe_PR_FAMINC i.site ideology lib_atlibtable con_atcontable, i(sitetable)
xi: xtreg raw_diff_ideology f3 f4 black hispanic asian gradschool i.oboe_PR_FAMINC i.site ideology lib_atlibtable con_atcontable, i(sitetable)



bysort sitetable: egen max_taxrich=max( ice_PR_DEF_TAXRICH)
bysort sitetable: egen max_cutprog=max( ice_PR_DEF_CUTPROG)
bysort sitetable: egen max_taxboth=max( ice_PR_DEF_TAXBOTH)
bysort sitetable: egen max_entitle=max( ice_PR_DEF_ENTITLE)
bysort sitetable: egen max_defense=max( ice_PR_DEF_DEFENSE)
bysort sitetable: egen max_fedsales=max( ice_PR_DEF_FEDSALES)
bysort sitetable: egen min_taxrich=min( ice_PR_DEF_TAXRICH)
bysort sitetable: egen min_cutprog=min( ice_PR_DEF_CUTPROG)
bysort sitetable: egen min_taxboth=min( ice_PR_DEF_TAXBOTH)
bysort sitetable: egen min_entitle=min( ice_PR_DEF_ENTITLE)
bysort sitetable: egen min_defense=min( ice_PR_DEF_DEFENSE)
bysort sitetable: egen min_fedsales=min( ice_PR_DEF_FEDSALES)

gen diff_taxrich= ice_PR_DEF_TAXRICH- oboe_PT_DEF_TAXRICH
gen diff_cutprog= ice_PR_DEF_CUTPROG- oboe_PT_DEF_CUTPROG
gen diff_taxboth= ice_PR_DEF_TAXBOTH- oboe_PT_DEF_TAXBOTH
gen diff_entitle= ice_PR_DEF_ENTITLE- oboe_PT_DEF_ENTITLE
gen diff_defense= ice_PR_DEF_DEFENSE- oboe_PT_DEF_DEFENSE
gen diff_fedsales= ice_PR_DEF_FEDSALES- oboe_PT_DEF_FEDSALES


* do not create!! these are all zero constants....
gen table_modlib_taxrich=max_taxrich<5 & min_taxrich>2
gen table_modlib_cutprog=max_cutprog<4 & min_cutprog>1
gen table_modlib_taxboth=max_taxboth<5 & min_taxboth>2
gen table_modlib_entitle=max_entitle<4 & min_entitle>1
gen table_modlib_defense=max_defense<5 & min_defense>2
gen table_modlib_fedsales=max_fedsales<5 & min_fedsales>2
gen table_modcon_taxrich=max_taxrich<4 & min_taxrich>1
gen table_modcon_cutprog=max_cutprog<5 & min_cutprog>2
gen table_modcon_taxboth=max_taxboth<4 & min_taxboth>1
gen table_modcon_entitle=max_entitle<5 & min_entitle>2
gen table_modcon_defense=max_defense<4 & min_defense>1
gen table_modcon_fedsales=max_fedsales<4 & min_fedsales>1
gen table_modlib_ideology=max_ideology< 2.62277 & min_ideology> 1.9502
gen table_modcons_ideology=max_ideology< 1.266466 & min_ideology>.011458

* so do these instead; conservative test of polarization since it includes tables with "extremists"....
gen table_lib_taxrich=min_taxrich>2
gen table_lib_cutprog=max_cutprog<4 
gen table_lib_taxboth=min_taxboth>2
gen table_lib_entitle=max_entitle<4 
gen table_lib_defense=min_defense>2
gen table_lib_fedsales=min_fedsales>2
gen table_con_taxrich=max_taxrich<4 
gen table_con_cutprog=min_cutprog>2
gen table_con_taxboth=max_taxboth<4 
gen table_con_entitle=min_entitle>2
gen table_con_defense=max_defense<4 
gen table_con_fedsales=max_fedsales<4 
gen table_lib_ideology=min_ideology>= 1.614586
gen table_cons_ideology=max_ideology< 1.614586 

bysort sitetable: gen table_keeper=_n==1

* table count
tab sitetable if table_lib_taxrich==1 & table_keeper==1 & diff_taxrich<.
tab sitetable if table_con_taxrich==1 & table_keeper==1 & diff_taxrich<.
tab sitetable if table_lib_cutprog==1 & table_keeper==1 & diff_cutprog<.
tab sitetable if table_con_cutprog==1 & table_keeper==1 & diff_cutprog<.
tab sitetable if table_lib_taxboth==1 & table_keeper==1 & diff_taxboth<.
tab sitetable if table_con_taxboth==1 & table_keeper==1 & diff_taxboth<.
tab sitetable if table_lib_entitle==1 & table_keeper==1 & diff_entitle<.
tab sitetable if table_con_entitle==1 & table_keeper==1 & diff_entitle<. 
tab  sitetable if table_lib_defense==1 & table_keeper==1 & diff_defense<.
tab  sitetable if table_con_defense==1 & table_keeper==1 & diff_defense<.
tab sitetable if table_lib_fedsales==1 & table_keeper==1 & diff_fedsales<.
tab sitetable if table_con_fedsales==1 & table_keeper==1 & diff_fedsales<.

* count at each table
tab sitetable if table_lib_taxrich==1 & diff_taxrich<.
tab sitetable if table_con_taxrich==1 & diff_taxrich<.
tab sitetable if table_lib_cutprog==1  & diff_cutprog<.
tab sitetable if table_con_cutprog==1 & diff_cutprog<. 
tab sitetable if table_lib_taxboth==1 & diff_taxboth<. 
tab sitetable if table_con_taxboth==1 & diff_taxboth<. 
tab sitetable if table_lib_entitle==1 & diff_entitle<. 
tab sitetable if table_con_entitle==1 & diff_entitle<. 
tab  sitetable if table_lib_defense==1 & diff_defense<. 
tab  sitetable if table_con_defense==1 & diff_defense<. 
tab sitetable if table_lib_fedsales==1 & diff_fedsales<.
tab sitetable if table_con_fedsales==1 & diff_fedsales<. 


* you see some polarization but not law like behavior....
tab diff_taxrich if table_lib_taxrich==1
tab diff_taxrich if table_con_taxrich==1
tab diff_cutprog if table_lib_cutprog==1
tab diff_cutprog if table_con_cutprog==1
tab diff_taxboth if table_lib_taxboth==1
tab diff_taxboth if table_con_taxboth==1
tab diff_entitle if table_lib_entitle==1
tab diff_entitle if table_con_entitle==1
tab  diff_defense if table_lib_defense==1
tab  diff_defense if table_con_defense==1
tab diff_fedsales if table_lib_fedsales==1
tab diff_fedsales if table_con_fedsales==1


*** here is the R function
*ptest<-function(x,n1,n2) {
*cat(sum(x)/(n2-n1), ", ")
*cat(((sum(x)/(n2-n1))-0.5)/sqrt(((sum(x)/(n2-n1))*(1-(sum(x)/(n2-n1))))/(n2-n1)), '/n')
*}


bysort sitetable: egen table_mean=total(ice_ideology)
replace table_mean=table_mean-ice_ideology
bysort sitetable: replace table_mean=table_mean/(_N-1)




